from selenium import webdriver
from selenium.webdriver.common.by import By
import time


class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://mail.163.com')

    def Login(self):
        # 跳转登录的frame页面
        # time.sleep(5)
        self.driver.implicitly_wait(5)
        s1 = self.driver.find_element(By.TAG_NAME, 'iframe')
        self.driver.switch_to.frame(s1)
        self.driver.switch_to.frame('x-URS-iframe1652625041618.235')

        # 输入账号密码
        self.driver.find_element(By.NAME, 'email').send_keys('h66920')
        self.driver.find_element(By.NAME, 'password').send_keys('h014..')
        self.driver.find_element(By.ID, 'dologin').click()

    def sendEmail(self):
        # 发送邮件
        time.sleep(5)
        self.driver.refresh()

        # 点击写信
        self.driver.find_element(By.ID, '_mail_component_149_149').click()
        # 定位收件人和主题
        self.driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt').send_keys('123@163.com')
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//header/div[2]/div[1]/div/div/input').send_keys('123')

        # 进入内容frame
        time.sleep(2)
        iframe = self.driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
        self.driver.switch_to.frame(iframe)
        time.sleep(2)
        self.driver.find_element(By.CLASS_NAME, 'nui-scroll').send_keys('123')

        # 返回主页面面发送
        time.sleep(2)
        self.driver.switch_to.default_content()
        # 点击发送按钮
        time.sleep(2)
        self.driver.find_elements(By.CLASS_NAME, 'nui-btn-text')[8].click()

    def quit(self):
        time.sleep(2)
        self.driver.quit()


if __name__ == '__main__':
    case2 = TestCase()
    case2.Login()
    case2.sendEmail()
    case2.quit()
